import { request, METHOD } from '@/service/http.service';
import { session, local } from '@/service/storage.service';
const { VITE_STORAGE_TOKEN } = import.meta.env;

export function sendCode(data = {}) {
  return request({
    url: '/api/register/verify',
    data,
    method: METHOD.POST,
  }).then(response => {
    if (response.data.status !== 200) {
      uni.showToast({
        title: response.data.msg,
      });
    } else {
      uni.showToast({
        title: '验证码已发送',
      });
    }
  });
}

export async function login(data = {}) {
  uni.showLoading({
    title: '数据请求中！',
  });
  const deviceInfo = await uni.getDeviceInfo();
  return request({
    url: '/api/login',
    data: {
      ...data,
      deviceId: deviceInfo.deviceId,
    },
    method: METHOD.POST,
  }).then(response => {
     uni.hideLoading()
     const { status, data, msg } = response.data;
     if (status === 200) {
      local.set(VITE_STORAGE_TOKEN, data.token);
    } else {
      uni.showToast({
        title: msg,
      });
    }

    return response;
  });
}

export function register(data = {}) {
  return request({
    url: '/api/register',
    data,
    method: METHOD.POST,
  });
}

export function getUserInfo() {
  return request({
    url: '/api/userinfo',
    method: METHOD.GET,
  });
}

export function logout() {
  return request({
    url: '/api/auth/logout',
    method: METHOD.POST,
  });
}


export function resetPassword(data = {}) {
  return request({
    url: '/api/register/reset',
    data,
    method: METHOD.POST,
  });
}

export function modifyPhone(params = {}) {
  return request({
    url: '/api/updatePhone',
    params,
    method: METHOD.GET,
  });
}

export function modifyEmail(params = {}) {
  
  return request({
    url: '/api/updateEmail',
    params,
    method: METHOD.GET,
  });
}

